<?php
/**
 * Created by PhpStorm.
 * User: wpf
 * Date: 2018/4/12
 * Time: 11:20
 */

namespace Cardlevel\Controller;

use Common\Controller\AdminbaseController;


class BunkController extends AdminbaseController
{
    protected $bunk_model;
    function _initialize() {
        parent::_initialize();
        $this->bunk_model = D("Bunk");
    }

    private function input_csv($handle)
    {
        $out = array ();
        $n = 0;
        while ($data = fgetcsv($handle, 10000))
        {
            $num = count($data);
            for ($i = 0; $i < $num; $i++)
            {
                $out[$n][$i] = mb_convert_encoding(trim($data[$i]),"UTF-8","GBK");
            }
            $n++;
        }
        return $out;
    }

    public function bunkimport(){
        $filename = $_FILES['file']['tmp_name'];
        if(empty ($filename))
        {
            $this->error('请选择要导入的CSV文件！');
            exit;
        }
        $handle = fopen($filename, 'r');
        $result = $this->input_csv($handle); //解析csv
        $len_result = count($result);
        if($len_result==0)
        {
            $this->error('没有任何数据！');
            exit;
        }
        array_shift($result);

//        dump($result);
//return;
        $count = 0;
        $pid = 3;
        $lid = 3;
        foreach ($result as $k => $v){
            if(count($v)>2) {//有正常数据
                //分别获取四块内容
                $lh = $v['0'];//楼号
                if ($lh) {
                    $temp = array(
                        'pid' => 3,
                        'name' => $lh . '楼',
                        'type' => 2
                    );
                    D('Bunk')->add($temp);
                    //添加之后更新当前的pid
                    $pid = D('Bunk')->where(array('pid' => 3, 'name' => $lh . '楼'))->getField('bunk_id');
                }

                $ch = $v['1'];//楼层号
                if ($ch) {
                    //从楼层号开始，pid需要动态改变赋值了
                    //逻辑也不对，只有当 楼号改变了  楼层号才需要改变
                    $temp = array(
                        'pid' => $pid,
                        'name' => $ch,
                        'type' => 3
                    );
                    D('Bunk')->add($temp);
                    $lid = D('Bunk')->where(array('pid' => $pid, 'name' => $ch))->getField('bunk_id');
                }

                $fh = $v['2'];//房间号
                if ($fh) {
                    //房号的pid出现问题，应为 记录的楼层号id  只有楼层号id更改了才会更改pid
                    //房号开始是必有内容，切房间内的床号需要进行分割
                    $fh = $fh . '号房间';
                    $temp = array(
                        'pid' => $lid,
                        'name' => $fh,
                        'type' => 4
                    );
                    D('Bunk')->add($temp);
                    $fid = D('Bunk')->where(array('pid' => $lid, 'name' => $fh))->getField('bunk_id');

                    $cw = $v['3'];//床位整体编号
                    $cwarr = str_split($cw);
                    $temps = array();
                    foreach ($cwarr as $value) {
                        $temps[] = array(
                            'pid' => $fid,
                            'name' => $value . '床位',
                            'type' => 5,
                            'totaluse' => 120
                        );
                    }
                    D('Bunk')->addAll($temps);
                }
                $count++;
            }
        }

        if($count){
            $this->success('已成功导入'.$count.'条信息！', U("link/index"));
        }else{
            $this->error('导入数据失败');
        }
    }


    function index(){
        $pid = I('pid', 0, 'intval');
        if($pid > 0) {
            $parent = $this->bunk_model->find($pid);
        } else {
            $parent = array('pid' => 0, 'name' => '一级');
        }
        $list = $this->bunk_model->where(array('pid' => $pid))->order(array("listorder"=>"asc"))->select();

        $this->assign('pid', $pid);
        $this->assign("list", $list);
        $this->assign("parent", $parent);
        $this->display();
    }

    function add(){
        $pid = I("pid", 0, 'intval');
        if($pid > 0) {
            $parent = $this->bunk_model->find($pid);
        } else {
            $parent = array('bunk_id' => 0, 'name' => '一级地区');
        }
        if(!$parent) {
            $this->error('父级信息不存在');
        }
        $this->assign('parent', $parent);
        $this->display();
    }

    function add_post(){
        if(IS_POST){
            if($this->bunk_model->create()) {
                if ($this->bunk_model->add()!==false) {
                    $this->success("添加成功！", U("link/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->bunk_model->getError());
            }

        }
    }

    function edit(){
        $bunk_id = I("bunk_id", 0, 'intval');
        $bunk = $this->bunk_model->find($bunk_id);
        if(!$bunk) {
            $this->error('信息不存在');
        }
        $pid = $bunk['pid'];
        if($pid > 0) {
            $parent = $this->bunk_model->find($pid);
        } else {
            $parent = array('bunk_id' => 0, 'name' => '一级地区');
        }
        if(!$parent) {
            $this->error('父级信息不存在');
        }
        $this->assign('parent', $parent);
        $this->assign('bunk', $bunk);
        $this->display();
    }

    function edit_post(){
        if (IS_POST) {
            $bunk_id = I("bunk_id", 0, 'intval');
            $bunk = $this->bunk_model->find($bunk_id);
            if(!$bunk) {
                $this->error('信息不存在');
            }
            if($this->bunk_model->create()) {
                if ($this->bunk_model->save()!==false) {
                    $this->success("保存成功！");
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error($this->bunk_model->getError());
            }
        }
    }

    //排序
    public function listorders() {
        if(parent::_listorders($this->bunk_model)) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

    //删除
    function delete(){
        $bunk_id = I("bunk_id", 0, 'intval');
        $bunk = $this->bunk_model->find($bunk_id);
        if(!$bunk) {
            $this->error('信息不存在');
        } else if($this->bunk_model->where(array('pid' => $bunk['bunk_id']))->count() > 0) {
            $this->error('该条记录有子记录, 不可删除!');
        } else {
            if ($this->bunk_model->delete($bunk_id)) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }
}